
#include <stdio.h>
#include "adc.h"
#include "device.h"
#include "gpio.h"
#include "nvic.h"

static  sj_int8  adc_device_open(struct file *file_node)
{
    return 0;
}

static  sj_int8  adc_device_write(struct file *file_node,sj_uint8 *data ,sj_int32 len,sj_loff_t * loff)
{
    struct adc_device_data *self_data = (struct adc_device_data *)(file_node->private_data);

    return 0;
}

static  sj_int8  adc_device_read(struct file *file_node,sj_uint8 *data ,sj_int32 len,sj_loff_t * loff)
{
    struct adc_device_data *self_data = (struct adc_device_data *)(file_node->private_data);
    sj_uint16 adc_value = 0;
    adc_value = peripheral_adc_start_get_data(&self_data->param);
    sj_kprintf("AD_Value:%d \r\n", adc_value);
    return 0;
}

static  sj_int8  adc_device_close(struct file *file_node)
{
    return 0;
}

static sj_int8 adc_device_probe(struct device *dev)
{
    sj_kprintf("start adc probr \r\n");
    sj_int8 ret =0;
    sj_int8 i=0;
    struct adc_device_data *self_data =(struct adc_device_data *)dev->private_data;
    if(!self_data)
    {
        ret =-1;
        goto out;
    }
    peripheral_adc_clock_config(self_data->adc_clk);

    struct rcc_apb_periph *adc_apb = self_data->adc_apb;
    while(adc_apb->apb != 0 && adc_apb->apb_offset != 0)
    {
        periph_clock_config(adc_apb);
        adc_apb++;
    }

    sj_gpio_init(&self_data->adc_gpio_init);

    peripheral_adc_init(&self_data->param, &self_data->param_channel);

out:
    return ret;
}

static struct file_operations adc_opt =
{
    adc_device_open,
    adc_device_close,
    NULL,
    adc_device_read,
    adc_device_write,
    NULL,
    NULL,
    NULL,
};

static struct device_id_table adc_id_table[]=
{
    {"adc1"},
    {NULL}
};

static struct device_driver adc1_driver={
    "adc1",
    adc_id_table,
    &adc_opt,
    adc_device_probe,
    NULL,
    NULL,
    NULL,
    NULL,
};

static DRIVER_MODULE_INIT(adc1_driver)

